package com.atjg.autocat.utils

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Color
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.SizeUtils
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.text.SimpleDateFormat

class Bitmap2FileUtils {
    companion object {
        @SuppressLint("SimpleDateFormat")
        fun saveBitmapFile(
            mContext: Context,
            bitmap: Bitmap,
            filePath: String,
            type: String,
            count: String
        ): String {
            val file = File(filePath);//将要保存图片的路径
            try {
                val boxCount = when (count) {
                    "1","01" -> {
                        "一号"
                    }

                    "2","02" -> {
                        "二号"
                    }

                    "3","03" -> {
                        "三号"
                    }

                    "4","04" -> {
                        "四号"
                    }

                    "5","05" -> {
                        "五号"
                    }

                    "6","06" -> {
                        "六号"
                    }

                    "7","07" -> {
                        "七号"
                    }
                    "8","08" -> {
                        "八号"
                    }
                    "9","09" -> {
                        "九号"
                    }
                    "10","0a" -> {
                        "十号"
                    }
                    "11","0b" -> {
                        "十号"
                    }
                    "12","0c" -> {
                        "十号"
                    }
                    else->{
                        "可回收"
                    }
                }

                val deviceName = SPUtils.getInstance().getString("deviceName", "")
                val msg = if (type == "net_first") {
                    "${boxCount}箱体 投递前  ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis())}  $deviceName"
                } else {
                    "${boxCount}箱体 投递后  ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis())}  $deviceName"
                }
                val newBitmap = ImageUtil.drawTextToLeftBottom(
                    mContext,
                    bitmap,
                    msg,
                    SizeUtils.dp2px(15f),
                    Color.parseColor("#FF0000"),
                    SizeUtils.dp2px(15f),
                    SizeUtils.dp2px(15f)
                )
                val bos = BufferedOutputStream(FileOutputStream(file));
                newBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
                bos.flush()
                bos.close()
                return filePath
            } catch (e: IOException) {
                e.printStackTrace()
                return ""
            }
        }

        fun saveBitmapFile(
            bitmap: Bitmap,
            filePath: String,
        ): String {
            val file = File(filePath);//将要保存图片的路径
            try {
                val bos = BufferedOutputStream(FileOutputStream(file))
                bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos)
                bos.flush()
                bos.close()
                return filePath
            } catch (e: IOException) {
                e.printStackTrace()
                return ""
            }
        }
    }
}